<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 150px;
            height: 150px;
            background-color: skyblue;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // var box =
        var x = {
            target: document.querySelector(".box"),
            dargstar() {
                var _this = this;
                this.target.onmousedown = function (e) {
                    var e = e || window.event
                    var posX = e.offsetX;
                    var posY = e.offsetY;
                    _this.posX = posX;
                    _this.posY = posY;

                    _this.dargmove();
                    _this.dargend();
                }
            },
            dargmove() {
                document.onmousemove = (e) => {
                    var e = e || window.event;
                    // console.log(this.posX, this.posY);
                    this.target.style.left = (e.pageX - this.posX) + "px"
                    this.target.style.top = (e.pageY - this.posY) + "px"
                    console.log(2);
                }


            },
            dargend() {
                document.onmouseup = function () {
                    document.onmousedown = null;
                    document.onmousemove = null;
                    document.onmouseup = null;
                    console.log(3);
                }
            }
        }
        x.dargstar();
    </script>
</body>

</html>